package cn.wolfcode.luowowo.website.util;

import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@AllArgsConstructor
@NoArgsConstructor
public class TookenResetUtil {

    public static final int USER_RESET_TOKEN = 0;  //用户信息token重置

    @Reference
    private IUserInfoService userInfoService;

    @Reference
    private IUserInfoRedisService userInfoRedisService;

    public static void tokenReset(HttpServletRequest request, HttpServletResponse response,Object object,int type){
        TookenResetUtil tokenResetUtil = new TookenResetUtil();
        switch (type){
            case USER_RESET_TOKEN:
                tokenResetUtil.resetUserToken(request, response, object);
                break;
        }
        tokenResetUtil = null;
    }

    private void resetUserToken(HttpServletRequest request, HttpServletResponse response,Object object){
        UserInfo userInfo = (UserInfo) object;
        UserInfo user = this.userInfoService.get(userInfo.getId());
        String token = CookieUtil.getToken(request, response);
        this.userInfoRedisService.setToken(token,user);
    }
}
